<template>
	<!-- 只读文件 -->
	<readOnlyInfo v-if="isDisable" ref="applyInfoRef" applyName="yspay" />
	<!-- 盛付通进件模板 -->
	<view v-if="!isDisable">
		<uni-collapse ref="collapse">
			<uni-collapse-item :show-animation="false" title="基础信息"  class="coll-title" :open="true">
				<view class="content">
					
					<JeePayForm text="商户类型" >
						<radio-group  class="radio-group" @change="publicSelect($event, 'merchantType')" >
							<label class="radio"><radio value="1" :checked="applyDetailInfo.merchantType == '1'" />小微商户</label>
							<label class="radio"><radio value="2" :checked="applyDetailInfo.merchantType == '2'"/>个体商户</label>
							<label class="radio"><radio value="3" :checked="applyDetailInfo.merchantType == '3'"/>企业商户</label>
						</radio-group>
					</JeePayForm>
					
					<template v-if="applyDetailInfo.merchantType != 1">
						<!-- 营业执照信息 小微不填 企业/个体工商户必填 -->
						<JeePayForm text="营业执照照片">
							 <JeepayUpLoad ocrFlag="license" :imgUrl="applyDetailInfo.licenseImg" @uploadSuccess="uploadSuccess($event, 'licenseImg')" @clear="clear('licenseImg')" />
						</JeePayForm>
						
						<JeePayForm text="营业执照编号" v-model:value="applyDetailInfo.licenseNo" />
						
						<JeePayForm text="营业执照起始有效期">
							<termOfValidity :defaultDate="applyDetailInfo.licenseEffectBegin" @publicSelect="publicSelect($event, 'licenseEffectBegin')" :isEnd="false" />
						</JeePayForm>
						
						<JeePayForm text="营业执照有效期截止">
							<termOfValidity :defaultDate="applyDetailInfo.licenseEffectEnd" @publicSelect="publicSelect($event, 'licenseEffectEnd')" />
						</JeePayForm>
					
					</template>
					
					<JeePayForm :text="applyDetailInfo.merchantType == 1 ? '商户名称':'营业执照名称'" v-model:value="applyDetailInfo.mchFullName" />
					<JeePayForm text="商户简称" v-if="applyDetailInfo.merchantType !== 1" v-model:value="applyDetailInfo.mchShortName" :start="false" />
					
					<JeePayForm text="法人身份证人像面照">
						<JeepayUpLoad ocrFlag="idCard" :imgUrl="applyDetailInfo.idcard1Img" @uploadSuccess="uploadSuccess($event, 'idcard1Img')" @clear="clear('idcard1Img')" />
					</JeePayForm>
					
					<JeePayForm text="法人身份证国徽面照片">
						<JeepayUpLoad ocrFlag="idCard" :imgUrl="applyDetailInfo.idcard2Img" @uploadSuccess="uploadSuccess($event, 'idcard2Img')" @clear="clear('idcard2Img')" />
					</JeePayForm>
					
					<JeePayForm text="法人手持身份证照片">
						<JeepayUpLoad :imgUrl="applyDetailInfo.idcardInHandImg" @uploadSuccess="uploadSuccess($event, 'idcardInHandImg')" @clear="clear('idcardInHandImg')" />
					</JeePayForm>
					
					<JeePayForm text="法人身份证姓名" v-model:value="applyDetailInfo.idcardName" />
					<JeePayForm text="法人身份证号" v-model:value="applyDetailInfo.idcardNo" />	
					<JeePayForm text="法人地址" v-model:value="applyDetailInfo.idcardAddress" />	
					<JeePayForm text="法人电话" v-model:value="applyDetailInfo.legalPersonPhone" />	
					
					<JeePayForm text="法人性别" >
						<radio-group  class="radio-group" @change="publicSelect($event, 'crpGender')" >
							<label class="radio"><radio value="0" :checked="applyDetailInfo.crpGender == '0'" />男</label>
							<label class="radio"><radio value="1" :checked="applyDetailInfo.crpGender == '1'"/>女</label>
						</radio-group>
					</JeePayForm>
					
					<JeePayForm text="法人职业" >
						<jeeDataPicker :code="applyDetailInfo.crpProfession" :localdata="professionList" @change="publicSelect($event, 'crpProfession')"  paramType="arr" />
					</JeePayForm>
					
					<JeePayForm text="法人身份证起始有效期">
						<termOfValidity :defaultDate="applyDetailInfo.idcardEffectBegin" @publicSelect="publicSelect($event, 'idcardEffectBegin')" :isEnd="false" />
					</JeePayForm>
					
					<JeePayForm text="法人身份证结束有效期">
						<termOfValidity :defaultDate="applyDetailInfo.idcardEffectEnd" @publicSelect="publicSelect($event, 'idcardEffectEnd')" />
					</JeePayForm>
					
				</view>
			</uni-collapse-item>
			<uni-collapse-item :show-animation="false" title="经营信息"  class="coll-title">
				<view class="content">
					
					<JeePayForm text="经营类目（MCC）" >
						<jeeDataPicker :code="codeBack.mccCode" :localdata="mcc" @change="publicSelect($event, 'mccCode')"  paramType="last" />
					</JeePayForm>
					
					<JeePayForm text="实际控制人姓名" v-model:value="applyDetailInfo.actualManager"  v-if="applyDetailInfo.merchantType != 1" />
					
					<JeePayForm text="是否为法人信息" >
						<radio-group  class="radio-group" @change="publicSelect($event, 'isLegalInfo')" >
							<label class="radio"><radio value="1" :checked="applyDetailInfo.isLegalInfo == '1'" />是</label>
							<label class="radio"><radio value="0" :checked="applyDetailInfo.isLegalInfo == '0'"/>否</label>
						</radio-group>
					</JeePayForm>
					
					<JeePayForm text="商户联系人姓名" v-model:value="applyDetailInfo.contactName" />
						
					<JeePayForm text="商户联系人手机号" v-model:value="applyDetailInfo.contactPhone" />
					<JeePayForm text="商户联系人邮箱" v-model:value="applyDetailInfo.contactEmail" />	
					
					<JeePayForm text="商户经营省市县" >
						<jeeDataPicker :code="codeBack.areaCode" :localdata="address" @change="publicSelect($event, 'areaCode')"  paramType="arr" />
					</JeePayForm>
					
					<JeePayForm text="商户经营地址" v-model:value="applyDetailInfo.address" />
					
					<JeePayForm text="门头照">
						<JeepayUpLoad :imgUrl="applyDetailInfo.storeOuterImg" @uploadSuccess="uploadSuccess($event, 'storeOuterImg')" @clear="clear('storeOuterImg')" />
					</JeePayForm>
					
					<JeePayForm text="店内环境照">
						<JeepayUpLoad :imgUrl="applyDetailInfo.storeInnerImg" @uploadSuccess="uploadSuccess($event, 'storeInnerImg')" @clear="clear('storeInnerImg')" />
					</JeePayForm>
					
					<JeePayForm text="收银台照">
						<JeepayUpLoad :imgUrl="applyDetailInfo.storeCashierImg" @uploadSuccess="uploadSuccess($event, 'storeCashierImg')" @clear="clear('storeCashierImg')" />
					</JeePayForm>
				</view>
			</uni-collapse-item>

			<uni-collapse-item :show-animation="false" title="结算账户"  class="coll-title">
				<view class="content">
					
					<JeePayForm text="账户类型">
						<radio-group class="radio-group" @change="publicSelect($event, 'settAccountType')" >
							<label class="radio"><radio value="C" :checked="applyDetailInfo.settAccountType == 'C'" />对私</label>
							<label class="radio"><radio value="B" :checked="applyDetailInfo.settAccountType == 'B'"/>对公</label>
						</radio-group>
					</JeePayForm>
					
					<JeePayForm text="结算类型">
						<radio-group class="radio-group" @change="publicSelect($event, 'settlementType')" >
							<label class="radio"><radio value="00" :checked="applyDetailInfo.settlementType == '00'" />结算至内部</label>
							<label class="radio"><radio value="01" :checked="applyDetailInfo.settlementType == '01'"/>结算至银行卡</label>
						</radio-group>
					</JeePayForm>

					<JeePayForm text="银行开户名" v-model:value="applyDetailInfo.settAccountName" />
		
					<template v-if="applyDetailInfo.settAccountType == 'B' && applyDetailInfo.merchantType == '3'">
						<JeePayForm text="开户许可证照片">
							<JeepayUpLoad :imgUrl="applyDetailInfo.companyAccountLicenseImg" @uploadSuccess="uploadSuccess($event, 'companyAccountLicenseImg')" @clear="clear('companyAccountLicenseImg')" />
						</JeePayForm>
					</template>
					
					<template v-else>
						<JeePayForm text="银行卡正面照">
							<JeepayUpLoad ocrFlag="bankCard" :imgUrl="applyDetailInfo.settAccountLicenseImg" @uploadSuccess="uploadSuccess($event, 'settAccountLicenseImg')" @clear="clear('settAccountLicenseImg')" />
						</JeePayForm>
						
						<JeePayForm text="银行卡反面照">
							<JeepayUpLoad :imgUrl="applyDetailInfo.settAccountBankImg2" @uploadSuccess="uploadSuccess($event, 'settAccountBankImg2')" @clear="clear('settAccountBankImg2')" />
						</JeePayForm>
					</template>
				
					<JeePayForm text="选择银行">
					  <text @tap="bankRef.open()">{{ applyDetailInfo.settAccountBankName || "选择银行" }}</text>
					</JeePayForm>

					<JeePayForm text="银行卡账号" v-model:value="applyDetailInfo.settAccountNo" />
					
					<JeePayForm text="开户行省市县" >
						<jeeDataPicker :code="codeBack.bankCode" :localdata="address" @change="publicSelect($event, 'settAccountBankBranchAreaCode')"  paramType="arr" />
					</JeePayForm>

					<JeePayForm text="选择支行">
					  <view @click="selectBranchBank">{{ applyDetailInfo.settAccountBankBranchName ? applyDetailInfo.settAccountBankBranchName : "请选择" }}</view>
					</JeePayForm>

					<JeePayForm text="开户行编号" v-model:value="applyDetailInfo.bankType" />
					<JeePayForm text="开户行行号" v-model:value="applyDetailInfo.openAccBanktype" />

					<JeePayForm text="银行预留手机号" v-model:value="applyDetailInfo.bankMobile" />

					<JeePayForm text="是否法人结算">
						<radio-group class="radio-group" @change="publicSelect($event, 'codeLegalPersonAcc')" >
							<label class="radio"><radio value="0" :checked="applyDetailInfo.codeLegalPersonAcc == 0" />非法人</label>
							<label class="radio"><radio value="1" :checked="applyDetailInfo.codeLegalPersonAcc == 1"/>法人</label>
						</radio-group>
					</JeePayForm>
					
					<template v-if="applyDetailInfo.codeLegalPersonAcc == '0'">

						<JeePayForm text="结算人身份证人像面照片">
							<JeepayUpLoad ocrFlag="idCard" :imgUrl="applyDetailInfo.settAccountIdcard1Img" @uploadSuccess="uploadSuccess($event, 'settAccountIdcard1Img')" @clear="clear('settAccountIdcard1Img')" />
						</JeePayForm>
						
						<JeePayForm text="结算人身份证国徽面照片">
							<JeepayUpLoad ocrFlag="idCard" :imgUrl="applyDetailInfo.settAccountIdcard2Img" @uploadSuccess="uploadSuccess($event, 'settAccountIdcard2Img')" @clear="clear('settAccountIdcard2Img')" />
						</JeePayForm>
						
						<JeePayForm text="结算人姓名" v-model:value="applyDetailInfo.unincorporatedName" />
						<JeePayForm text="结算人身份证号" v-model:value="applyDetailInfo.settAccountIdcardNo" />
						<JeePayForm text="结算人证件有效期">
							<termOfValidity :defaultDate="applyDetailInfo.settAccountIdcardEffectEnd" @publicSelect="publicSelect($event, 'settAccountIdcardEffectEnd')" />
						</JeePayForm>
						<JeePayForm text="结算人身份证地址" v-model:value="applyDetailInfo.authAddress" />
					</template>

				</view>
			</uni-collapse-item>
			
		</uni-collapse>
		
	</view>
	
	<!-- 选择银行 -->
	<SelectBank ref="bankRef" :list="bankInfoList" value="label" @choiceValue="updataBank" />
	
	<!-- 选择支行 -->
	<sftSelectBankBranch ref="sftSelectBankBranckRef" @bankBranckInfo="bankBranckInfo" :bankList="bankBranchList" field="bankName" />
	
	<!-- 上传图片，提交审核 状态4触发 -->
	<ysEvent ref="ysEventRef" />
	
</template>

<script setup>
import {ref , reactive, computed, toRaw, inject } from 'vue'
import address from '@/components/applyJson/ysfAddress.json' // 地址
import mcc from '@/components/applyJson/jeePayMcc.json' // 行业
import JeepayUpLoad from '@/components/JeepayUpLoad/JeepayUpLoad.vue' // 图片上传
import readOnlyInfo from '../../components/readOnlyInfo.vue' // 只读文件
import tool from '@/util/tool.js' // 工具类函数
import JeePayForm from '@/components/applyComponents/JeePayForm.vue' // 通用左右结构布局
import termOfValidity from '@/components/applyComponents/termOfValidity.vue' // 选择证件有效期
import jeeDataPicker from '@/components/applyComponents/dataPicker.vue' // 通用级联选择
import ysEvent from './ysEvent.vue'
import { $bankBranchList } from "@/http/apiManager.js";
import sftSelectBankBranch from "@/components/sftSelectBank/sftSelectBankBranch.vue"; // 选择支行
import SelectBank from "../../components/SelectBank.vue"; // 选择银行组件
import bankInfoList from './ysBank.json' // 银行列表

const channelInfo = inject('channelInfo')

const emit = defineEmits(['applyHandle'])
const ysEventRef = ref(null) // 进件完毕之后的操作页面
const bankRef = ref(null)

// 状态分类: 0-未发起、 1-审核中、 2-进件成功、 3-驳回待修改、 4-待验证、 5-待签约
// 当状态为 1 2 4 5 时要开启只读模式，1 3 不开启
let isDisable = ref(false)
if (!['0', '3', '8'].includes(channelInfo.state)) isDisable.value = true

// 商户进件详细信息（JSON类型字符串）
let applyDetailInfo = reactive({})

const sftSelectBankBranckRef = ref(null) // 选择支行组件
const bankBranchList = ref([]) // 支行列表，用于交付上方的组件渲染并选择

// 选择银行回调
const updataBank = val => applyDetailInfo.settAccountBankName = val.label

// 通用选择函数
const publicSelect = (e, key) => {
	applyDetailInfo[key] = e.detail.value
	if (key === 'isLegalInfo') {
		if (e.detail.value == 1) {
			applyDetailInfo.contactName = applyDetailInfo.idcardName
			applyDetailInfo.contactPhone = applyDetailInfo.legalPersonPhone
			applyDetailInfo.settAccountName = applyDetailInfo.idcardName
			applyDetailInfo.contactsAddr = applyDetailInfo.idcardAddress
		} else {
			applyDetailInfo.contactName = applyDetailInfo.contactPhone = ''
			applyDetailInfo.contactsAddr = applyDetailInfo.settAccountName = ''
		}
	} else if (key == 'crpProfession') {
		applyDetailInfo.crpProfession = e.detail.value[0]
	} else if (key == 'areaCode') {
		applyDetailInfo.mercProv = e.detail.text[0]
		applyDetailInfo.mercCity = e.detail.text[1]
		applyDetailInfo.mercArea = e.detail.text[2]
	} else if (key == 'settAccountBankBranchAreaCode') {
		applyDetailInfo.openAccProvince = e.detail.text[0]
		applyDetailInfo.openAccCity = e.detail.text[1]
		
		// 选择开户行省市区的回调
		$bankBranchList(channelInfo.mchNo, channelInfo.code, {
		  cityCode: e.detail.value[1], // 市编码
		  bankAliasCode: applyDetailInfo.settAccountBankName, // 银行名称
		}).then((res) => {
		  bankBranchList.value = res.bizData;
		  applyDetailInfo.settAccountBankBranchName = ""; // 请求完毕赋空
		})
	}
}

// 选择支行
const selectBranchBank = () => {
  // 空值校验
  if (bankBranchList.value.length == 0)  return uni.showToast({ title: "支行列表为空", icon: "none" });
  sftSelectBankBranckRef.value.open();
}

// 选择支行完毕
const bankBranckInfo = val => {
	applyDetailInfo.settAccountBankBranchName = val.bankName
	applyDetailInfo.bankType = val.bankType
	applyDetailInfo.openAccBanktype = val.bankCode
}

// 法人职业
const professionList = [
	{ value: '1', text: '机关团体企事业单位' },
	{ value: '2', text: '公共事业' },
	{ value: '3', text: '农牧业' },
	{ value: '4', text: '交通运输业' },
	{ value: '5', text: '餐旅业' },
	{ value: '6', text: '建筑工程业' },
	{ value: '7', text: '制造业' },
	{ value: '8', text: '医疗保健业' },
	{ value: '9', text: '娱乐业' },
	{ value: '10', text: '其他' },
]

// 上传图片成功
const uploadSuccess = (res, name) => {
	applyDetailInfo[name] = res.data
	if (res.ocrInfo && Object.keys(res.ocrInfo).length < 1) return false
	if (name == 'settAccountIdcard1Img') {
		applyDetailInfo.unincorporatedName = res.ocrInfo.idcardName
		applyDetailInfo.settAccountIdcardNo = res.ocrInfo.idcardNo
		applyDetailInfo.authAddress = res.ocrInfo.idcardAddress
		return
	}
	if (name == 'settAccountIdcard2Img') {
		applyDetailInfo.settAccountIdcardEffectEnd = res.ocrInfo.idcardEffectEnd
		return
	}
	Object.assign(applyDetailInfo, res.ocrInfo) // ocr信息回显，字段一致直接合并
}
// 清除图片  仅前端
const clear = (name, url) => applyDetailInfo[name] = ''

// 发起进件  注意后面的formCheck参数，表单校验要用
const launchApply = isTempData => {
	emit(
	'applyHandle', 
	{name: 'yspay',isTempData, applyDetailInfo, formCheck: [applyDetailInfo.merchantType, applyDetailInfo.codeLegalPersonAcc, applyDetailInfo.settAccountType]},
	)
}

// 只读组件信息
let applyInfoRef = ref(null)

// 行业类型，地理位置回显
let codeBack = reactive({
	areaCode: '', // 商户地理位置回显
	mccCode: '', // 行业回显
	bankCode: '', // 银行地址回显
})

// 合并信息方法, 职能读取，和信息回显，通过此方法合并
const assignObj = (obj) => {
	Object.assign(applyDetailInfo, obj)
	
	if (channelInfo.state == 4) ysEventRef.value.setApplyInfo()
	
	if(!isDisable.value) {
		// 行业类型，地理位置回显
		codeBack.areaCode = obj.areaCode ? obj.areaCode[2] : null
		codeBack.bankCode = obj.settAccountBankBranchAreaCode ? obj.settAccountBankBranchAreaCode[2] : null
		codeBack.mccCode = obj.mccCode
	} else {
		// 以下用于只读信息页
		obj.mccText = tool.mccBack(obj.mccCode, mcc, 'last') // 行业码转文字
		obj.areaText = tool.addressBack(obj.areaCode, address) // 地址码转文字
		obj.bankText = tool.addressBack(obj.settAccountBankBranchAreaCode, address)
		obj.crpProfessionText = professionList.find(item => item.value == obj.crpProfession).text // 职业
		// 传值给只读组件
		if(applyInfoRef.value) applyInfoRef.value.getApplyInof(obj)
	}
}

defineExpose({ launchApply, assignObj })

</script>

<style scoped lang="scss">
@import '../../static/information.scss';
</style>